﻿<template>
    <div class="esBudget-container">
        <el-card shadow="hover">
            <el-form :model="state.queryParams" ref="queryForm" :inline="true">
                <el-form-item :label-width='70' label="商户名称">
                    <el-select :clearable='true' class='ml10' v-model="state.queryParams.merchantId"
                               placeholder="请选择" filterable>
                        <el-option v-for='item in state.merchantDict' :key='item.value' :label='item.label'
                                   :value='item.value'/>
                    </el-select>
                </el-form-item>
                <el-form-item :label-width='70' label="产品名称" prop="name">
                    <el-input placeholder="产品名称" clearable class='ml10' @keyup.enter="handleQuery"
                              v-model="state.queryParams.name"/>
                </el-form-item>
                <el-form-item>
                    <el-button-group>
                        <el-button type="primary" icon="ele-Search" @click="handleQuery"
                                   v-auth="'esBudgetProduct:query'"> 查询
                        </el-button>
                        <el-button icon="ele-Refresh" @click="resetQuery" v-auth="'esBudgetProduct:query'"> 重置
                        </el-button>
                    </el-button-group>
                </el-form-item>
            </el-form>
        </el-card>

        <el-card shadow="hover" style="margin-top: 8px">
            <div style="margin-bottom: 8px">
                <el-button type="success" plain icon="ele-Plus" @click="openAddEsBudgetProduct"
                           v-auth="'esBudgetProduct:add'"> 新增
                </el-button>
            </div>
            <el-table :data="state.data" style="width: 100%" v-loading="state.loading" tooltip-effect="light"
                      row-key="id"
                      border stripe highlight-current-row>
                <el-table-column header-align="center" prop="merchantName" label="商户名称" width="100"
                                 show-overflow-tooltip/>
                <el-table-column header-align="center" prop="name" label="产品名称" min-width="420"
                                 show-overflow-tooltip/>
                <el-table-column header-align="center" prop="province" width="90" label="省份" align="center"
                                 show-overflow-tooltip/>
                <el-table-column header-align="center" prop="operatorPlatform" label="运营商" width="90" align="center"
                                 show-overflow-tooltip>
                    <template #default="scope">
                        <el-tag type="success" v-if="scope.row.operatorPlatform === 1">移动</el-tag>
                        <el-tag type="warning" v-else-if="scope.row.operatorPlatform === 2">联通</el-tag>
                        <el-tag type="info" v-else-if="scope.row.operatorPlatform === 3">电信</el-tag>
                        <el-tag type="danger" v-else-if="scope.row.operatorPlatform === 4">广电</el-tag>
                        <el-tag type="success" v-else-if="scope.row.operatorPlatform === 5">移动虚拟运营商</el-tag>
                        <el-tag type="warning" v-else-if="scope.row.operatorPlatform === 6">联通虚拟运营商</el-tag>
                        <el-tag type="info" v-else-if="scope.row.operatorPlatform === 7">电信虚拟运营商</el-tag>
                        <el-tag type="danger" v-else>未知</el-tag>
                    </template>
                </el-table-column>
                <el-table-column header-align="center" prop="informationFee" label="信息费(元)" align="right"
                                 width="120" show-overflow-tooltip>
                    <template #default="scope">
                        {{ (scope.row.informationFee / 100.00).toFixed(2) }}
                    </template>
                </el-table-column>
                <el-table-column header-align="center" prop="incomePrice" label="收入价格(元)" align="right" width="120"
                                 show-overflow-tooltip>
                    <template #default="scope">
                        {{ (scope.row.incomePrice / 100.00).toFixed(2) }}
                    </template>
                </el-table-column>
                <el-table-column header-align="center" prop="businessExpenses" label="商务费(元)" align="right"
                                 min-width="130" show-overflow-tooltip>
                    <template #default="scope">
                        {{ (scope.row.businessExpenses / 100.00).toFixed(2) }}
                    </template>
                </el-table-column>
                <el-table-column header-align="center" prop="businessPrice" label="扣除商务价格(元)" align="right"
                                 min-width="130" show-overflow-tooltip>
                    <template #default="scope">
                        {{ (scope.row.businessPrice / 100.00).toFixed(2) }}
                    </template>
                </el-table-column>
                <el-table-column header-align="center" prop="customerService" label="客服费(元)" align="right"
                                 min-width="130" show-overflow-tooltip>
                    <template #default="scope">
                        {{ (scope.row.customerService / 100.00).toFixed(2) }}
                    </template>
                </el-table-column>
                <el-table-column header-align="center" prop="refundRate" label="退费率" align="right" min-width="130"
                                 show-overflow-tooltip>
                    <template #default="scope">
                        {{ scope.row.refundRate + "%" }}
                    </template>
                </el-table-column>
                <el-table-column header-align="center" prop="userNumber" label="推广用户数" align="right"
                                 min-width="130" show-overflow-tooltip/>
                <el-table-column header-align="center" prop="promotionAmount" label="推广金额(元)" align="right"
                                 min-width="130" show-overflow-tooltip>
                    <template #default="scope">
                        {{ (scope.row.promotionAmount / 100.00).toFixed(2) }}
                    </template>
                </el-table-column>
                <el-table-column header-align="center" prop="keep" label="留存数" align="right" min-width="130"
                                 show-overflow-tooltip/>
                <el-table-column header-align="center" prop="retentionRate" label="留存率" align="right" min-width="130"
                                 show-overflow-tooltip>
                    <template #default="scope">
                        {{ scope.row.retentionRate + "%" }}
                    </template>
                </el-table-column>
                <el-table-column header-align="center" prop="moonNumber" label="几个月" align="right" min-width="130"
                                 show-overflow-tooltip/>
                <el-table-column header-align="center" prop="createTime" label="创建时间" width="160"
                                 show-overflow-tooltip/>
                <el-table-column label="操作" width="140" align="center" fixed="right" show-overflow-tooltip>
                    <template #default="scope">
                        <el-button size="small" text type="warning" @click="openBudgetStatement(scope.row)"
                                   v-auth="'esBudgetProduct:update'"> 查看报表
                        </el-button>
                        <el-button size="small" text type="primary" @click="openEditEsBudgetProduct(scope.row)"
                                   v-auth="'esBudgetProduct:update'"> 重新生成
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
                           :page-sizes="[20, 50, 100]" v-model:current-page="state.queryParams.pageNum" background
                           v-model:page-size="state.queryParams.pageSize"
                           layout="total, sizes, prev, pager, next, jumper"
                           :total="state.total"/>
            <QueryEsBudgetStatement ref="queryEsBudgetStatementRef" :title="state.editTitle" width="100%"/>
            <EditEsBudgetProduct ref="editEsBudgetProductRef" :title="state.editTitle"/>
        </el-card>
    </div>
</template>

<script lang="ts" setup name="esBudget">

import {onMounted, onUnmounted, reactive, ref} from 'vue';
import {ElMessageBox, ElMessage} from 'element-plus';
import mittBus from '/@/utils/mitt';
import QueryEsBudgetStatement from '/src/views/exocytosis/budget/budgetStatement/index.vue';
import EditEsBudgetProduct from '/@/views/exocytosis/budget/budgetProduct/component/editEsBudgetProduct.vue';

import {EsBudgetProduct, sysBaseDict} from '/@/api/models';
import SysEsBudgetProductApi from '/@/api/exocytosis/budget';
import SysEsMerchantApi from '/@/api/exocytosis/merchant';

const queryEsBudgetStatementRef = ref<InstanceType<typeof QueryEsBudgetStatement>>();
const editEsBudgetProductRef = ref<InstanceType<typeof EditEsBudgetProduct>>();
const state = reactive({
    loading: false,
    data: [] as Array<EsBudgetProduct>,
    total: 0 as any,
    queryParams: {
        merchantId: undefined,
        channelId: undefined,
        name: undefined,
        pageNum: 1,
        pageSize: 20,
    },
    editTitle: '',
    merchantDict: [] as sysBaseDict[],
    channelDict: [] as sysBaseDict[],
});

onMounted(async () => {
    await merchantDictQuery();
    mittBus.on('submitRefresh', () => {
        handleQuery();
    });
});

onUnmounted(() => {
    mittBus.off('submitRefresh');
});

// 查询操作
const handleQuery = async () => {
    state.loading = true;
    const res = await SysEsBudgetProductApi().queryRecord(state.queryParams);
    state.data = res.data.result.items ?? [];
    state.total = res.data.result?.total;
    state.loading = false;
};

// 重置操作
const resetQuery = () => {
    // 1. 批量重置为undefined
    const keepKeys: string[] = ['pageNum', 'pageSize',]; // 声明需要保留的字段
    Object.keys(state.queryParams).forEach(key => {
        if (!keepKeys.includes(key))
            state.queryParams[key] = undefined;
    });
    // 2. 数据
    state.data = [];
};

// 打开新增页面
const openAddEsBudgetProduct = () => {
    state.editTitle = '添加';
    editEsBudgetProductRef.value?.openDialog({
        chargingMode: 2,
        blackListFilterStatus: 1,
        httpMethod: 'GET',
        mimeType: '',
        orderResult: 0,
        requestTimes: 0,
        userLimit: 0,
        arrivalType: 1,
        requestLimit: 0
    }, 'add');
};

// 打开生成报表页面
const openBudgetStatement = (row: EsBudgetProduct) => {
    state.editTitle = '报表';
    queryEsBudgetStatementRef.value?.openDialog(row, 'edit');
};

// 打开编辑页面
const openEditEsBudgetProduct = (row: EsBudgetProduct) => {
    state.editTitle = '添加';
    editEsBudgetProductRef.value?.openDialog(row);
};

// 删除
const delEsBudgetProduct = (row: any) => {
    ElMessageBox.confirm(`确定删除：【${row.name}】?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    })
        .then(async () => {
            let res = await SysEsBudgetProductApi().deletePost({id: row.id});
            if (res.data.resCode != '0000')
                ElMessage.error(res.data.resMessage);
            else
                ElMessage.success("删除成功");
            await handleQuery();
        })
        .catch(() => {
        });
};

// 改变页面容量
const onHandleSizeChange = (val: number) => {
    state.queryParams.pageSize = val;
    handleQuery();
};

// 改变页码序号
const onHandleCurrentChange = (val: number) => {
    state.queryParams.pageNum = val;
    handleQuery();
};

// 商户列表
const merchantDictQuery = async () => {
    await SysEsMerchantApi().queryAll().then((res) => {
        if (res.data.resCode === '0000')
            state.merchantDict = res.data.result ?? [];
    });
};

</script>
